var Search = Class.create();
Search.prototype = {
initialize : function() {
                 this.index = 0;
                 this.url = '';
                 this.data = null;
                 this.playurl = '';
                 this.fc = swfobject.getObjectById("flashContent");
                 this.is_pause = 0;
             },
do_search : function(){
                try{
                    var key = $('search_text').value;
                    var param = {
key : encodeURI(key)
                    };
                    new Ajax.Request(
                            "/music/search",
                            {
parameters:param,
method: 'get',
onSuccess : ajaxsearch.search_result
});
}catch(e){
}
},
setmute : function(){
          },
strfsize: function(size) {
              var ords = "KMGTPE", i = 0, remain = 0;
              size = 0 + size;
              if (size < 0) return '0';
              if (size < 973) return '' + size;
              do {
                  remain = size & 1023;
                  size = parseInt(size / 1024);
                  if (size < 973)
                      break;
                  i++;
              } while (true);
              if (size < 9 || (size == 9 && remain < 973)) {
                  if ((remain = Math.floor(((remain * 5) + 256) / 512)) >= 10) {
                      size++;
                      remain = 0;
                  }
                  return '' + size + '.' + remain + ords.charAt(i);
              }
              if (remain >= 512) size++;
              return '' + size + ords.charAt(i);
          },
search_result: function(ret){
                   try{
                       var data = ret.responseText.evalJSON();
                   }
                   catch(e){
                       ajaxsearch.stand_error(data["msg"]);
                       return false;
                   }

                   try{
                       if(data["status"]){
                           ajaxsearch.data = data["msg"];
                           var info = '<div class="rs"><ul>'
                               + '<li class="t">歌曲名称</li>'
                               + '<li class="ar">演唱者</li>'
                               + '<li class="al">专辑</li>'
                               + '<li class="si">体积</li>'
                               + '<li class="tt">类型</li>'
                               + '</ul>';
                           var index = 0;
                           data["msg"].each(function(o){
                                   var size = 
                                   info += "<ul>"
                                   + "<li class='t'><a href='javascript:;' onclick='ajaxsearch.ready2play(\"" + o["urls"] + "\"," + index + ")'> " + o["title"] + "</a></li>"
                                   + "<li class='ar'>" + o["artist"] + "</li>"
                                   + "<li class='al'>" + o["album"] + "</li>"
                                   + "<li class='si'>" + ajaxsearch.strfsize(o["size"]) + "</li>"
                                   + "<li class='tt'>" + o["type"] + "</li>"
                                   + "</ul>";
                                   index += 1;
                                   });
                           info += "</div>";
                           $('result').innerHTML = info;
                       }
                       else{
                           ajaxsearch.stand_error(data["msg"]);
                           return false;
                       }
                   }catch(e){
                       ajaxsearch.stand_error(e.message);
                   }
               },
ready2play: function(url, index){
                ajaxsearch.url = url;
                ajaxsearch.index = index;
                nurl = ajaxsearch.data[index]["urls"];
                param = {
                    'url' : nurl
                };
                new Ajax.Request(
                        "/music/geturl",
                        {
parameters:param,
method: 'post',
onSuccess : ajaxsearch.play
});
},
stand_error : function(info){
                  $('result').innerHTML = "<div class='error'>" + info + "</div>";
              },
play : function(data){
           try{
               var d = data.responseText.evalJSON();
               if(d["status"]){
                   ajaxsearch.playurl = d["msg"][0]["urls"];
                   ajaxsearch.letsplay(1);
               }
               else{
                   ajaxsearch.stand_error(d["msg"]);
                   return false;
               }
           }
           catch(e){
               ajaxsearch.stand_error(data["msg"]);
               return false;
           }
       },
letsplay:function(force){
             if(!ajaxsearch.playurl){
                 ajaxsearch.stand_error("没有选择播放曲目!");
                 return false;
             }
             if(force == 1){
                 try{
                     // ui 函数可能不存在,这里不强求
                     set_play();
                 }catch(e){ console.log(e.message); }
                 ajaxsearch.is_pause = false;
                 if(!ajaxsearch.fc){
                     ajaxsearch.fc = swfobject.getObjectById("flashContent");
                 }
                 ajaxsearch.fc.set_src(ajaxsearch.playurl);
                 // ajaxsearch.fc.set_src("/music/index?url=" + ajaxsearch.playurl);
                 ajaxsearch.fc.play();
                 return;
             }
             else{
                 try{
                     // ui 函数可能不存在,这里不强求
                     set_pause();
                 }catch(e){ console.log(e.message); }
                 if(ajaxsearch.is_pause){
                     ajaxsearch.fc.resume();
                     ajaxsearch.is_pause = 0;
                 }else{
                     ajaxsearch.fc.pause();
                     ajaxsearch.is_pause = 1;
                 }
             }
         },
showvolumn :function(show){
                //var ab_x = Element.positionedOffset('sound_btn');
                var ab_x = parseInt($('sound_btn').offsetLeft) + parseInt(parseInt($('sound_btn').getStyle('width')) / 2);
                var ab_y = parseInt($('sound_btn').offsetTop) + parseInt($('sound_btn').getStyle('height'));

                if(Element.visible($('slider_control')) || show == true){
                    console.log('1');
                    Element.setStyle($('slider_control'), {'display' : 'none'});
                }else{
                    console.log('2');
                    Element.setStyle($('slider_control'), {marginLeft: ab_x + "px", marginTop: ab_y + "px", 'position' : 'absolute', 'display' : ''});
                }
            },
setVolumn : function(offset){
                console.log(offset / 100);
                ajaxsearch.fc.set_volumn(offset / 100);
            }

};
var ajaxsearch = new Search();
